﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using KetBanBonPhuong;
using System.IO;

public partial class UserControls_Group : System.Web.UI.UserControl
{
    private static int iPage = 1;
    private static int GroupID = 5;
    private static int iPageSize = 6;
    private static DataTable dtPost;
    private static int[] ListIDPost = new int[6];
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            //String StrGroup = Request.QueryString["GroupID"];
            //if (String.IsNullOrEmpty(StrGroup))
            //{
            //    Response.Redirect("Home.aspx");
            //}
            //if (!int.TryParse(StrGroup, out GroupID))
            //{
            //    Response.Redirect("Home.aspx");
            //}

            //String StrPage = Request.QueryString["Page"];
            //if (String.IsNullOrEmpty(StrPage))
            //{
            //    iPage = 1;
            //}
            //if (!int.TryParse(StrPage, out iPage))
            //{
            //    iPage = 1;
            //}
            Session["UserID"] = 1;
            GetData();
        }
    }
    protected void Button_Post_Click(object sender, EventArgs e)
    {

    }

    protected void GetData()
    {
        int iCountPost = iPageSize;
        bool hasNext = true;
        dtPost = Post.GetAllPostOfGroup(GroupID);
        if (dtPost.Rows.Count < iPage * iPageSize)
        {
            iCountPost = dtPost.Rows.Count - (iPage - 1) * iPageSize;
            hasNext = false;
        }
        else
        {
            hasNext = true;
        }

        String ControlName;
        for (int i = 0; i < iCountPost; ++i)
        {
            ListIDPost[i] = (int)dtPost.Rows[(iPage - 1) * iPage + i]["post_id"];
            ControlName = "Literal" + (i + 1).ToString();
            Literal LiteralContent = (Literal)FindControl(ControlName);
            Panel PanelContent = (Panel)FindControl("Panel" + (i + 2).ToString());
            LiteralContent.Text = LoadData(ListIDPost[i]);
            PanelContent.Visible = true;
        }
        Panel8.Visible = true;
        HyperLink_NextPage.NavigateUrl = "../Group.aspx?GroupID=" + GroupID.ToString() + "Page=" + (iPage + 1).ToString();
        if (!hasNext)
        {
            for (int i = iCountPost; i < iPageSize; ++i)
            {
                ListIDPost[i] = -1;
                Literal LiteralContent = (Literal)FindControl("Literal" + (i + 1).ToString());
                Panel PanelContent = (Panel)FindControl("Panel" + (i + 2).ToString());
                LiteralContent.Text = "";
                PanelContent.Visible = false;
            }
            Panel8.Visible = false;
        }
    }

    private String LoadData(int PostID)
    {
        String StrContent = "";
        String StrComment = "";
        String StrCommentItem = "";
        Post post = Post.GetInfo(PostID);
        StrContent = File.ReadAllText(Server.MapPath("~/UserControls/Template/Post.htm"));
        StrCommentItem = File.ReadAllText(Server.MapPath("~/UserControls/Template/CommentItem.htm"));
        StrContent = StrContent.Replace("$UserName", User.GetInfo((int)Session["UserID"]).UserName);
        StrContent = StrContent.Replace("$Content", post.Text);
        StrContent = StrContent.Replace("$Title", post.Title);
        StrContent = StrContent.Replace("$CreateDate", String.Format("{0,2:00}:{1,2:00}:{2,2:00} - {3,2:00}/{4,2:00}/{5,4}",
                post.Created_date.Hour, post.Created_date.Minute, post.Created_date.Second,
                post.Created_date.Day, post.Created_date.Month, post.Created_date.Year));
        StrContent = StrContent.Replace("$Profile", "Profile.aspx?UserID=" + Group.GetInfo(GroupID).User_id);
        if (!String.IsNullOrEmpty(User.GetInfo(Group.GetInfo(GroupID).User_id).Avatar.Trim()))
        {
            StrContent = StrContent.Replace("$Avatar", "Files/Avatar" + User.GetInfo(Group.GetInfo(GroupID).User_id).Avatar.Trim());
        }
        else
        {
            StrContent = StrContent.Replace("$Avatar", "Files/Avatars/default.jpg");
        }

        DataTable dtComment = CommentPost.GetAll(PostID);
        DataView dv = dtComment.DefaultView;
        dv.Sort = "created_date ASC";
        dtComment = dv.ToTable();
        foreach (DataRow dtRow in dtComment.Rows)
        {
            User user = User.GetInfo(Member.GetInfo((int)dtRow["member_id"]).Member_user_id);

            String StrTemp = StrCommentItem;
            StrTemp = StrTemp.Replace("$UserName", user.UserName);
            StrTemp = StrTemp.Replace("$Comment", dtRow["text"].ToString());
            DateTime time = (DateTime)dtRow["created_date"];

            StrTemp = StrTemp.Replace("$CreateDate", String.Format("{0,2:00}:{1,2:00}:{2,2:00} - {3,2:00}/{4,2:00}/{5,4}",
                time.Hour, time.Minute, time.Second,
                time.Day, time.Month, time.Year));
            StrTemp = StrTemp.Replace("$Profile", "Profile.aspx?UserID=" + user.UserID);

            if (!String.IsNullOrEmpty(user.Avatar))
            {
                StrTemp = StrTemp.Replace("$Avatar", "Files/Avatar" + user.Avatar);
            }
            else
            {
                StrTemp = StrTemp.Replace("$Avatar", "Files/Avatars/default.jpg");
            }

            StrComment += StrTemp;
        }

        StrContent = StrContent.Replace("$Comment", StrComment);
        return StrContent;
    }

    protected void Page_Prerender(object sender, EventArgs e)
    {

    }
    protected void Button_Comment1_Click(object sender, EventArgs e)
    {
        Comment(1, TextBox_Comment1.Text.Trim());
    }
    protected void Button_Comment2_Click(object sender, EventArgs e)
    {
        Comment(2, TextBox_Comment2.Text.Trim());
    }
    protected void Button_Comment3_Click(object sender, EventArgs e)
    {
        Comment(3, TextBox_Comment3.Text.Trim());
    }
    protected void Button_Comment4_Click(object sender, EventArgs e)
    {
        Comment(4, TextBox_Comment4.Text.Trim());
    }
    protected void Button_Comment5_Click(object sender, EventArgs e)
    {
        Comment(5, TextBox_Comment5.Text.Trim());
    }
    protected void Button_Comment6_Click(object sender, EventArgs e)
    {
        Comment(6, TextBox_Comment6.Text.Trim());
    }

    protected void Comment(int IndexPost, String ContentComment)
    {
        if (ContentComment.Length > 0)
        {
            CommentPost comment = new CommentPost();
            comment.Post_id = ListIDPost[IndexPost - 1];
            Status status = Status.GetInfo(comment.Post_id);
            comment.Member_id = Member.GetMemberID((int)Session["UserID"], GroupID);
            comment.Text = ContentComment;
            comment.Created_date = DateTime.Now;
            CommentPost.Insert(comment);
            ((Literal)FindControl("Literal" + IndexPost.ToString())).Text = LoadData(ListIDPost[IndexPost - 1]);
        }
    }

}
